package me.sdau.spider.kafkaprocess.comm.utils;


import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import me.sdau.spider.kafkaprocess.kafka.dto.ConfigDTO;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

/**
 * @author zj
 * @date 2018/3/29 15:00
 */
@Slf4j
public class FileUtils {
    private static Long lastModified = 0L;

    public static List<ConfigDTO> getIndex() {
        File path = new File(System.getProperty("user.dir"));
        File file = new File(path, "topicConfig.yml");
        try (InputStream input = new FileInputStream(file)) {
            Yaml yaml = new Yaml();
            JSONArray jsonArray = yaml.loadAs(input, JSONArray.class);
            return jsonArray.toJavaList(ConfigDTO.class);
        } catch (Exception e) {
            log.error("读取配置文件失败[{}]", e.getMessage(), e);
            e.printStackTrace();
            return null;
        }
    }

    public static boolean isModified() {
        boolean isModified = false;

        File path = new File(System.getProperty("user.dir"));
        File file = new File(path, "topicConfig.yml");

        if (file.lastModified() > lastModified) {
            log.info("配置文件已生效");
            lastModified = file.lastModified();
            isModified = true;
        } else {
            log.info("配置文件未修改");
        }
        return isModified;
    }

}
